package cn.gxljc.technology.store_management.service.superadminservice.components;

import cn.gxljc.technology.store_management.entity.Account;
import cn.gxljc.technology.store_management.entity.AccountInfo;
import cn.gxljc.technology.store_management.entity.AccountPermissions;
import cn.gxljc.technology.store_management.mapper.AccountInfoMapper;
import cn.gxljc.technology.store_management.mapper.AccountMapper;
import cn.gxljc.technology.store_management.mapper.AccountPermissionsMapper;
import cn.gxljc.technology.store_management.service.loginservice.modules.LoginServiceMapper;
import cn.gxljc.technology.store_management.service.superadminservice.modules.UserManagement;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 该类主要实现用户管理接口
 */
@Component
public class UserManagementImpl implements UserManagement {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private AccountInfoMapper accountInfoMapper;

    @Resource
    private AccountPermissionsMapper accountPermissionsMapper;

    @Resource
    private LoginServiceMapper loginServiceMapper;

    private boolean isAccountExist(long accountID) {
        AccountInfo accountInfo = accountInfoMapper.selectByAccountID(accountID);
        return accountInfo != null;
    }

    @Override
    public Integer createAccount(long accountID, String password, String accountName) {
        if (password == null || accountName == null) {//非空判断
            System.out.println("UserManagementImpl/createAccount/参数为空");
            return -1;
        }

        //查看账号是否存在
        Account account1 = accountMapper.selectByAccountID(accountID);
        if (account1 != null) {
            System.out.println("UserManagementImpl/createAccount/账号重复");
            return -2;
        }

        //创建账号
        Account account = new Account();
        account.setAccountID(accountID);
        account.setPassword(password);
        loginServiceMapper.createAccount(accountID, password);

        //设置对应的名称
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setAccountID(accountID);
        accountInfo.setAccountName(accountName);
        accountInfo.setPhoneNumber("");
        accountInfo.setEmail("");
        accountInfoMapper.deleteAccountInfo(accountID);//先删除后增加
        accountInfoMapper.insertAccountInfo(accountInfo);

        return 0;
    }

    @Override
    public Integer deleteAccount(long accountID) {
        //判断被删除的账号是否存在
        Account account = accountMapper.selectByAccountID(accountID);
        if (account == null) {
            System.out.println("UserManagementImpl/deleteAccount/账号不存在");
            return -2;
        }

        //删除账号操作
        //删除权限
        accountPermissionsMapper.deleteAccountPermissions(accountID);
        //删除信息
        accountInfoMapper.deleteAccountInfo(accountID);
        //最后删除账号
        boolean b = accountMapper.deleteAccount(accountID);
        if (b) return 0;
        else return -1;
    }

    @Override
    public Integer setAuthority(long accountID, Integer type) {
        if (type == null) {
            System.out.println("UserManagementImpl/setAuthority/参数为空");
            return -1;
        }//非空判断

        //判断权限类型是否正确
        List<Integer> typeList = new ArrayList<>();
        typeList.add(0);//超级管理员
        typeList.add(1);//仓库管理员
        typeList.add(3);//卖家
        if (!typeList.contains(type)) {
            System.out.println("UserManagementImpl/setAuthority/权限类型不正确");
            return -3;
        }

        //判断用户是否存在
        boolean accountExist = isAccountExist(accountID);
        if (!accountExist) {
            System.out.println("UserManagementImpl/setAuthority/账号不存在");
            return -2;
        }

        //设置权限
        AccountPermissions accountPermissions = new AccountPermissions();
        accountPermissionsMapper.updatePermissions(accountID, type);
        return 0;
    }
}
